package app.srong.controller;

import app.srong.service.PracticeService;
import app.srong.service.PracticeTopicService;
import app.srong.vo.Practice;
import app.srong.vo.PracticeTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 打字统计视图
 */
@RestController
@RequestMapping("/static")
public class StaticController {

    @Autowired
    PracticeService practiceService;
    @Autowired
    PracticeTopicService practiceTopicService;


    @PostMapping("/makeWord")
    public void makeWord(@RequestBody Practice practice) {
        practice.setCreateTime(LocalDateTime.now());
        practiceService.makeWord(practice);

    }

    /**
     * 开始一次练习
     * @param usbTopicId 练习对应主题的ID
     * @return 一次练习ID
     */
    @GetMapping("/startSubTopic")
    public Integer startSubTopic(Integer usbTopicId) {

        PracticeTopic practiceTopic = new PracticeTopic();
        practiceTopic.setSubTopicId(usbTopicId);
        practiceTopic.setStartTime(LocalDateTime.now());

        practiceTopicService.insert(practiceTopic);
        return practiceTopic.getId();
    }

    /**
     * 结束一次练习
     * @return 各项练习的统计时间
     */
    @GetMapping("/endSubTopic")
    public PracticeTopic endSubTopic(Integer usbTopicId) {


        return practiceTopicService.endSubTopic(usbTopicId);
    }


}
